<template>
  <global-header :user='currentuser'></global-header>
  <Loading v-if="isloading" text="正在加载中" background="rgba(0,0,0,0.4)"></Loading>
  <router-view></router-view>
<footer class="text-center">
    <ul class="list-inline mb-0">
        <li class="list-inline-item">@写作专栏</li>
        <li class="list-inline-item">课程</li>
        <li class="list-inline-item">文档</li>
        <li class="list-inline-item">联系</li>
        <li class="list-inline-item">更多</li>
    </ul>
</footer>
</template>

<script lang="ts">
import { defineComponent, computed, onMounted, watch } from 'vue'
import { useStore } from 'vuex'
import Loading from './components/Loading.vue'
import { GlobalDataProps } from './store'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap'
import GlobalHeader from './components/GlobalHeader.vue'
import axios from 'axios'
import createMessage from './createMessage'

export default defineComponent({
  name: 'App',
  components: {
    GlobalHeader,
    Loading
  },
  setup () {
    const store = useStore<GlobalDataProps>()
    const currentuser = computed(() =>

      store.state.user
    )// 这个返回的是一个ref对象。
    const token = computed(() => {
      return store.state.token
    })
    const isloading = computed(() =>
      store.state.loading
    )
    const error = computed(() =>
      store.state.error
    )//

    watch(() => error.value.status, () => {
      const { status, message } = error.value
      console.log(error)

      if (status && message) {
        createMessage(message, 'error')
      }
    })

    onMounted(() => {
      if (!currentuser.value.isLogin && token.value) {
        axios.defaults.headers.common.Authorization = `Bearer ${token.value}`
        store.dispatch('fetchCurrentUser').then(() => {
          console.log(currentuser.value)// 是undedfined
        })
      }
    })

    return {
      isloading,
      currentuser,
      error
    }
  }
}
)
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
